/**
 * @file filter
 * @author liaoxuezhi@baidu.com
 */
/* eslint fecs-indent: [0, "space", 2, 2] */

import * as React from 'react';
import {
  Component,
  PropTypes
} from 'react';
import {Renderer, filter} from './renderer';
import OnExpression from './hoc/on-expression';
import Html from '../widgets/html';
import * as cx from 'classnames';
import * as assign from 'object-assign';

@Renderer({
  name: 'tpl',
  desc: '...',
  test: (path, config) => /(?:^|\/)(?:tpl)$/.test(path) || /(?:^|\/)form-item$/.test(path) && config.type == 'tpl'
})
@OnExpression
export default class Tpl extends Component {
  static propTypes = {
    className: PropTypes.string,
    tpl: PropTypes.string.isRequired,
    data: PropTypes.object.isRequired
  };

  static contextTypes = {
    can: PropTypes.func
  };

  static defaultProps = {
    tpl: '',
    data: {}
  };

  render() {
    const {
      className,
      inline,
      block,
      locals,
      data,
      tpl
    } = this.props;

    return (
      <Html
        className={cx(className, {
          block,
          inline
        })}
        html={filter(tpl, assign({
          acl: {
            can: this.context.can
          }
        }, locals, data))}
      />
    );
  }
}
